package com.neusoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import com.neusoft.po.Coupon;
import com.neusoft.po.CommonResult;
import com.neusoft.service.CouponService;

@RefreshScope
@RestController
@RequestMapping("/CouponController")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @GetMapping("/listCouponByUserId/{userId}")
    public CommonResult<List> listCouponByUserId(@PathVariable String userId) throws Exception {
        return new CommonResult(200, "success", couponService.listCouponByUserId(userId));
    }
    
    @GetMapping("/listCouponByAmount/{userId}/{amount}")
    public CommonResult<List> listCouponByUserId(@PathVariable String userId, @PathVariable double amount) throws Exception {
        return new CommonResult(200, "success", couponService.listCouponByAmount(userId, amount));
    }

    @PostMapping("/addCoupon/{userId}/{couponName}/{threshold}/{discount}")
    public CommonResult<Integer> addCoupon(@PathVariable("userId") String userId,
                                           @PathVariable("couponName") String couponName,
                                           @PathVariable("threshold") double threshold,
                                           @PathVariable("discount") double discount) throws Exception{
        return new CommonResult(200, "success", couponService.addCoupon(userId, couponName, threshold, discount));
    }

    @PostMapping("/useCoupon/{userId}/{couponId}")
    public CommonResult<Integer> useCoupon(@PathVariable String userId, @PathVariable Integer couponId) throws Exception {
        return new CommonResult(200, "success", couponService.useCoupon(userId, couponId));
    }
}